<template>
  <div>
    <!-- 弹出层的头部区域 -->
    <van-nav-bar title="频道管理">
      <template #right>
        <van-icon
          name="cross"
          size="0.37333334rem"
          color="white"
          @click="closePoup"
        />
      </template>
    </van-nav-bar>
    <!-- 我的频道 -->
    <div class="my-channel-box">
      <div class="channel-title">
        <span>我的频道
          <span class="small-title">
            点击{{show?'删除':'进入'}}频道
          </span>
        </span>
        <span @click="show=!show">{{show?'完成':'编辑'}}</span>
      </div>
      <!-- 我的频道列表 -->
      <van-row type="flex">
        <van-col
          span="6"
          v-for="item in list"
          :key="item.id"
          @click="entfn(item.id)"
        >
          <div class="channel-item van-hairline--surround">
            {{item.name}}
            <!-- 删除的徽标 -->
            <van-badge
              color="transparent"
              class="cross-badge"
              v-show="show && item.id!==0"
            >
              <template #content>
                <van-icon
                  name="cross"
                  class="badge-icon"
                  color="#cfcfcf"
                  size="0.32rem"
                  @click="del(item.id)"
                />
              </template>
            </van-badge>
          </div>
        </van-col>
      </van-row>
    </div>

    <!-- 更多频道 -->
    <div class="more-channel-box">
      <div class="channel-title">
        <span>点击添加更多频道：</span>
      </div>
      <!-- 更多频道列表 -->
      <van-row type="flex">
        <van-col
          span="6"
          v-for="(item,index) in unselect"
          :key="item.id"
          @click="add(item,item.id,index)"
        >
          <div class="channel-item van-hairline--surround">{{item.name}}</div>
        </van-col>
      </van-row>
    </div>
  </div>
</template>

<script>
import { getAllChannels } from '@/api/channel.js'
// 导入局部组件必须在scrip里面导入,在App里面导入
export default {
  // 组件名称
  name: 'pingdao',
  // 组件参数 接收来自父组件的数据
  props: {
    // 已经选择的频道
    list: {
      type: Array,
      required: true
    }
  },
  // 接收局部注册的组件
  components: {},
  // 组件数据
  data () {
    return {
      // 所有频道
      arr: [],
      show: false,
      ary: ''
    }
  },
  async created () {
    const arr = await getAllChannels()
    this.arr = arr.data.data.channels
  },
  methods: {
    //   关闭弹出层
    closePoup () {
      this.$emit('close')
      setTimeout(() => {
        if (this.show) {
          this.show = !this.show
        }
      }, 100);

      //   this.$router.push({
      //     path: '/layout/home'
      //   })
    },
    // 删除
    del (id) {
      //   this.ary = item
      this.$emit('delmit', id)
    },
    // 增加
    add (item) {
      //   this.list.push(item)

      if (this.show) {
        this.$emit('add', item)
      } else {
        this.$toast('请点击之后再进行编辑')
      }
    },
    //点击进入频道
    entfn (id) {
      // console.log(id);
      if (this.show == false) {
        this.$emit('entfn', id)
      }
    }
  },
  computed: {
    // 计算出不同的数组 频道数据
    unselect () {
      return this.arr.filter(obj => {
        if (this.list.some(item => item.id === obj.id)) {
          return false
        } else {
          return true
        }
      })
    }

  }
}
</script>
<style lang='less' scoped>
.van-popup,
.popup-container {
  background-color: transparent;
  height: 100%;
  width: 100%;
}

.popup-container {
  display: flex;
  flex-direction: column;
}

.pop-header {
  height: 90px;
  background-color: #007bff;
  color: white;
  text-align: center;
  font-size: 14px;
  position: relative;
  .title {
    width: 100%;
    position: absolute;
    bottom: 15px;
  }
}

.pop-body {
  flex: 1;
  overflow: scroll;
  padding: 8px;
  background-color: white;
}

.my-channel-box,
.more-channel-box {
  .channel-title {
    display: flex;
    justify-content: space-between;
    font-size: 14px;
    line-height: 28px;
    padding: 0 6px;
  }
}

.channel-item {
  font-size: 12px;
  text-align: center;
  line-height: 36px;
  background-color: #fafafa;
  margin: 5px;
}

/*删除的微标 */
.cross-badge {
  position: absolute;
  right: -3px;
  top: 0;
  border: none;
}

/*提示文字 */
.small-title {
  font-size: 10px;
  color: gray;
}
</style>
